var URL = {};
var location = window.location;
var decode = window.decodeURIComponent;

// http://localhost:8888/h5/build/account.html?from=aaa&id=111#%21/view/bill
var href = URL.href = decode(location.href); // 同上(所有与均为字符串)
var host = URL.host = decode(location.host); // localhost:8888
var hostname = URL.hostname = decode(location.hostname); // localhost
var search = URL.search = decode(location.search); // ?from=aaa&id=111
var hash = URL.hash = decode(location.hash); // #%21/view/bill
var origin = URL.origin = decode(location.origin); // http://localhost
var pathname = URL.pathname = decode(location.pathname); // /h5/build/account.html
var port = URL.port = decode(location.port); // 8888
var protocol = URL.protocol = decode(location.protocol); // http:
var arr = pathname.split('/');
var filename = URL.filename = arr[arr.length - 1] || ''; // account.html
var extname = URL.extname = filename.split('.').reverse()[0]; // html
var basename = URL.basename = filename.replace(new RegExp('\\.' + extname + '$'), ''); // account
var url = URL.url = origin + pathname; // http://localhost/h5/build/account.html
var dirname = '/'; // build
for (var i = 1; i < arr.length - 1; i++) {
    dirname += arr[i] + '/';
}
URL.dirname = dirname || '';

//根据访问的url 生成参数对象
var params = URL.params = {};
var param = href;
if (param.indexOf("?") != -1) {
    var param = param.substr(param.indexOf("?")+1);
    param = param.split("&");
    URL.params['channel'] = '';
    for (var i = 0; i < param.length; i++) {
        let key = unescape(param[i].split("=")[0]);
        let value =  unescape(param[i].split("=")[1])
        if(key == 'channel') {
          var arr = value.split("-");
          if(arr.length!=2) {
            value = value + '-' + basename;
          }
        }
        URL.params[key] = value;
    }
}


URL.exploreParams = function(url) {
  //根据访问的url 生成参数对象
  var params = {};
  var param = url.split('#')[0];
  if (param.indexOf("?") != -1) {
      var param = param.substr(param.indexOf("?")+1);
      param = param.split("&");
      for (var i = 0; i < param.length; i++) {
          let key = unescape(param[i].split("=")[0]);
          let value =  unescape(param[i].split("=")[1])
          params[key] = value;
      }
  }
  return params;
}

URL.jump = function(path,force) {
  if(force) {
    var timestamp = (new Date()).getTime();
    if(path.indexOf('?')>0) {
      location.href = origin+'/'+path + '&t=' + timestamp;
    } else {
      location.href = origin+'/'+path + '?t=' + timestamp;
    }
  } else {
    location.href = origin+'/'+path;
  }
}
URL.redirect = function(host,path) {
  location.href = protocol+'//'+host+'/'+path;
}

module.exports = URL;
